﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.MailClient.Win.Controls;
using DevExpress.MailClient.Win.Modules;
using DevExpress.MailClient.Win.Modules.Donnees;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraNavBar;
using DevExpress.XtraBars.Helpers;
using DevExpress.XtraBars;
using DevExpress.XtraRichEdit;
using DevExpress.XtraPrinting;

namespace DevExpress.MailClient.Win {
    public partial class frmMain : RibbonForm,IMain {

        public Guid Support { get; set; }
        public string Format { get; set; }
        public string Media { get; set; }
        NavBarGroupTagObject navbargrouptagDonne = new NavBarGroupTagObject("Donnees", typeof(Donnees));
        NavBarGroupTagObject navbargrouptagTarifs = new NavBarGroupTagObject("Tarifs", typeof(Tarifs));
        NavBarGroupTagObject navbargrouptagPlans = new NavBarGroupTagObject("Plans", typeof(Plans));
        NavBarGroupTagObject navbargrouptagCampagnes = new NavBarGroupTagObject("Campagnes", typeof(Campagne));

   






        MailType currentMailType = MailType.Inbox;
        ModulesNavigator modulesNavigator;
        internal FilterColumnsManager FilterColumnManager;
        ZoomManager zoomManager;
        List<BarItem> AllowCustomizationMenuList = new List<BarItem>();
        public frmMain() {
            InitializeComponent();
            InitNavBarGroups();
            SkinHelper.InitSkinGallery(rgbiSkins);
            RibbonButtonsInitialize();
            modulesNavigator = new ModulesNavigator(ribbonControl1, pcMain);
            modulesNavigator.ChangeGroup(navBarControl1.ActiveGroup, this);
            NavigationInitialize();
            SetPageLayoutStyle();
            repositoryItemComboBox1.Items.Add(5);
            repositoryItemComboBox1.Items.Add(10);
            repositoryItemComboBox1.Items.Add(15);
            //((ComboBoxEdit) repositoryItemComboBox1.ac).SelectedIndex = 1;
            //var a = modulesNavigator.CurrentModule.ModuleName;
          
        }

        //cette fonction doit servir lorsque on appuie sur un button dans la navbar et que et selectionne le module
        void NavigationInitialize() {
            foreach(NavBarGroup group in navBarControl1.Groups) {
                BarButtonItem item = new BarButtonItem(ribbonControl1.Manager, group.Caption)
                {
                    Tag = @group,
                    Glyph = @group.SmallImage
                };
                item.ItemClick += new ItemClickEventHandler(item_ItemClick);
                bsiNavigation.ItemLinks.Add(item); 
            }
       
        }

        void item_ItemClick(object sender, ItemClickEventArgs e) {
            navBarControl1.ActiveGroup = (NavBarGroup)e.Item.Tag;
        }
        void RibbonButtonsInitialize() {
             InitBarButtonItem(bbiNew, TagResources.NewCampaign, Properties.Resources.NewItemDescription);
             InitBarButtonItem(bbiNewCible, TagResources.NewCible, Properties.Resources.NewCible);

             InitBarButtonItem(bbiNewPlan, TagResources.NewPlan, Properties.Resources.NewCible);

             InitBarButtonItem(bbiCharger, TagResources.ChargerDonnees);
             InitBarButtonItem(bbiSave, TagResources.SavePlan);
             InitBarButtonItem(bbiProcess, TagResources.ProcessPlan);

             InitBarButtonItem(bbiOptimizer, TagResources.Optimization);

             InitBarButtonItem(bbiOptimizeTv, TagResources.OptimizationTv);
             InitBarButtonItem(bbiOptimizeRd, TagResources.OptimizationRd);
             InitBarButtonItem(bbiOptimizePr, TagResources.OptimizationPr);

            List<BarButtonItem> items = new List<BarButtonItem>();
            items.Add(bbiSubjectColumn);
            items.Add(bbiFromColumn);
            items.Add(bbiDateColumn);
            items.Add(bbiPriorityColumn);
            items.Add(bbiAttachmentColumn);
            items.Add(bbiDate);
            FilterColumnManager = new FilterColumnsManager(items);
            AllowCustomizationMenuList.Add(bsiNavigation);
            AllowCustomizationMenuList.Add(rgbiSkins);
            ribbonControl1.Toolbar.ItemLinks.Add(rgbiSkins);
        }

        
        void InitBarButtonItem(BarButtonItem buttonItem, object tag) {
            InitBarButtonItem(buttonItem, tag, string.Empty);
        }
        void InitBarButtonItem(DevExpress.XtraBars.BarButtonItem buttonItem, object tag, string description) {
            buttonItem.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(bbi_ItemClick);
            buttonItem.Hint = description;
            buttonItem.Tag = tag;
        }
        void InitNavBarGroups() {
            nbgCampagne.Tag = new NavBarGroupTagObject("Campagne", typeof(Campagne));
            nbgContacts.Tag = new NavBarGroupTagObject("Donnees", typeof(Donnees));
            nbgCible.Tag = new NavBarGroupTagObject("Cible", typeof(Cible));
        }
     
     
   
        string GetModuleName() {
            if(string.IsNullOrEmpty(modulesNavigator.CurrentModule.PartName)) return CurrentModuleName;
            return string.Format("<b>{0}</b>", CurrentModuleName);
        }
        string GetModulePartName() {
            if(string.IsNullOrEmpty(modulesNavigator.CurrentModule.PartName)) return null;
            return string.Format(" - {0}", modulesNavigator.CurrentModule.PartName);
        }
        private void navBarControl1_ActiveGroupChanged(object sender, DevExpress.XtraNavBar.NavBarGroupEventArgs e) {
            object data = GetModuleData((NavBarGroupTagObject)e.Group.Tag);
            modulesNavigator.ChangeGroup(e.Group, data);
        }
        private void bbi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            modulesNavigator.CurrentModule.ButtonClick(string.Format("{0}", e.Item.Tag));
        }
     
      
      
        private void frmMain_KeyDown(object sender, KeyEventArgs e) {
            modulesNavigator.CurrentModule.SendKeyDown(e);   
        }
        protected object GetModuleData(NavBarGroupTagObject tag) {
             return null;
           
        }
        private void navBarControl1_NavPaneStateChanged(object sender, EventArgs e) {
            //ucCalendar1.StateChanged(navBarControl1.OptionsNavPane.ActualNavPaneState);
            SetPageLayoutStyle();
        }
      

      

        private void galleryControlGallery1_ItemClick(object sender, GalleryItemClickEventArgs e) {
            if(TagResources.OpenCalendar.Equals(e.Item.Tag)) {
                ribbonControl1.HideApplicationButtonContentControl();
                this.Refresh();
            }
            modulesNavigator.CurrentModule.ButtonClick(string.Format("{0}", e.Item.Tag));
        }

        private void backstageViewControl1_ItemClick(object sender, BackstageViewItemEventArgs e) {
            if(modulesNavigator.CurrentModule == null) return;
            modulesNavigator.CurrentModule.ButtonClick(string.Format("{0}", e.Item.Tag));
        }
        void SetPageLayoutStyle() {
            bbiNormal.Down = navBarControl1.OptionsNavPane.NavPaneState == NavPaneState.Expanded;
            bbiReading.Down = navBarControl1.OptionsNavPane.NavPaneState == NavPaneState.Collapsed;
        }

        private void bbiNormal_ItemClick(object sender, ItemClickEventArgs e) {
            if(bbiNormal.Down) navBarControl1.OptionsNavPane.NavPaneState = NavPaneState.Expanded;
            else 
                bbiNormal.Down = true;
        }

        private void bbiReading_ItemClick(object sender, ItemClickEventArgs e) {
            if(bbiReading.Down) navBarControl1.OptionsNavPane.NavPaneState = NavPaneState.Collapsed;
            else 
                bbiReading.Down = true;
        }

        private void rgbiCurrentView_GalleryInitDropDownGallery(object sender, InplaceGalleryEventArgs e) {
            e.PopupGallery.GalleryDropDown.ItemLinks.Add(bbiManageView);
            e.PopupGallery.GalleryDropDown.ItemLinks.Add(bbiSaveCurrentView);
            e.PopupGallery.SynchWithInRibbonGallery = true;
        }
        
        private void rgbiCurrentViewTasks_GalleryItemClick(object sender, GalleryItemClickEventArgs e) {
            modulesNavigator.CurrentModule.ButtonClick(string.Format("{0}", e.Item.Tag));
        }

        private void ucCalendar1_VisibleChanged(object sender, EventArgs e) {
            if(ucCalendar1.Visible)
                ucCalendar1.UpdateTreeListHeight();
        }
        
        private void bvtiPrint_SelectedChanged(object sender, BackstageViewItemEventArgs e) {
            if(backstageViewControl1.SelectedTab == bvtiPrint)
                this.printControl1.InitPrintingSystem();
        }
        private void ribbonControl1_BeforeApplicationButtonContentControlShow(object sender, EventArgs e) {
            /*if(backstageViewControl1.SelectedTab == bvtiPrint) backstageViewControl1.SelectedTab = bvtiInfo;
            bvtiPrint.Enabled = CurrentRichEdit != null || CurrentPrintableComponent != null;
            bvtiExport.Enabled = CurrentExportComponent != null;*/
        }
        public IPrintable CurrentPrintableComponent { get { return modulesNavigator.CurrentModule.PrintableComponent; } }
        public IPrintable CurrentExportComponent { get { return modulesNavigator.CurrentModule.ExportComponent; } }
        public RichEditControl CurrentRichEdit { get { return modulesNavigator.CurrentModule.CurrentRichEdit; } }
        public string CurrentModuleName { get { return modulesNavigator.CurrentModule.ModuleName; } }

        private void ribbonControl1_ShowCustomizationMenu(object sender, RibbonCustomizationMenuEventArgs e) {
            e.CustomizationMenu.InitializeMenu();
            if(e.Link == null || !AllowCustomizationMenuList.Contains(e.Link.Item))
                e.CustomizationMenu.RemoveLink(e.CustomizationMenu.ItemLinks[0]);
        }

        private void navBarItem4_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            modulesNavigator.ChangeGroup(navbargrouptagDonne,this);
        }

        private void navBarItem5_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            modulesNavigator.ChangeGroup(navbargrouptagTarifs, this);
        }

        private void barEditItem2_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        private void ucPlaning1_Load(object sender, EventArgs e)
        {

        }


        private void navBarItem2_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            modulesNavigator.ChangeGroup(navbargrouptagPlans, this);
        }

        private void navBarItem1_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            modulesNavigator.ChangeGroup(navbargrouptagCampagnes, this);
        }

        private void backstageViewControl1_Click(object sender, EventArgs e)
        {
            }

    

        private void nbiRealTime_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
        }


        private void navBarControl1_Click(object sender, EventArgs e)
        {

        }

        private void nbiDtructureAud_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
        }

        private void nbiPartAud_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
        }

        private void nbiPenetration_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
        }

        private void nbiProfilPen_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
        }

       
    }
}
